package com.example.date;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hej
 * @date 2024/7/22 15:50
 */
public class WeekTest3 {
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        int currentWeekOfYear = now.get(ChronoField.ALIGNED_WEEK_OF_YEAR);
        ZoneId zoneId = ZoneId.systemDefault();
        List<WeekDTO> weeks = new ArrayList<>();
        int startWeek = currentWeekOfYear < 8 ? 1 : currentWeekOfYear - 7;
        LocalDate firstWeekStartDate = now.with(ChronoField.ALIGNED_WEEK_OF_YEAR, startWeek)
                .with(TemporalAdjusters.previousOrSame(LocalDate.of(now.getYear(),
                        1, 1).getDayOfWeek()));
        int weeksToGenerate = Math.min(9, currentWeekOfYear);

        for (int i = 0; i < weeksToGenerate; i++) {
            LocalDate weekStart = firstWeekStartDate.plusWeeks(i);
            LocalDate weekEnd = weekStart.plusDays(6);
            WeekDTO weekDTO = new WeekDTO();
            weekDTO.setWeek(String.format("WK%02d", startWeek + i));
            weekDTO.setBeginDate(weekStart.atStartOfDay().atZone(zoneId).toLocalDateTime());
            weekDTO.setEndDate(weekEnd.atTime(23, 59, 59).atZone(zoneId).toLocalDateTime());
            weeks.add(weekDTO);
            System.out.println(weekDTO);
        }
    }
}
